package com.umeng.soexample.dao;

import android.app.Service;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.text.TextUtils;

public class USerService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 饿汉式单例
     */
    private static USerService instance = new USerService();
    private USerService(){}
    public static USerService getInstance(){
        //单例校验双重锁
        if(instance == null){
                synchronized (USerService.class){
                if (instance == null){
                    instance = new USerService();
                }
            }
        }
        return instance;
    }

    private Context context;
    public void init(Context context){
        this.context = context;
        dbUtils = new DBUtils(context);
    }

    /**
     * 获取到数据库工具类对象
     */
    private DBUtils dbUtils;



    //增加数据
    public long insert(String tableNmae, ContentValues values){
        if (TextUtils.isEmpty(tableNmae) || values == null){
            return 0;
        }
        long insert = dbUtils.insert(tableNmae, values);
        return insert;
    }

    //删除数据
    public int delete(String tableNmae, String whereClause, String[] whereArgs){
        if (TextUtils.isEmpty(tableNmae) || TextUtils.isEmpty(whereClause) || whereArgs == null){
            return 0;
        }
        int delete = dbUtils.delete( tableNmae, whereClause, whereArgs);
        return delete;
    }

    //修改数据
    public int update(String tableNmae, ContentValues values, String whereClause, String[] whereArgs){
        if (TextUtils.isEmpty(tableNmae) || TextUtils.isEmpty(whereClause) || whereArgs == null || values == null){
            return 0;
        }
        int update = dbUtils.update(tableNmae, values, whereClause, whereArgs);
        return update;
    }

    //查询数据
    public Cursor query(String tableNmae, String whereClause, String[] whereArgs){
        if (TextUtils.isEmpty(tableNmae)){
            return null;
        }
        Cursor query = dbUtils.query(tableNmae, whereClause, whereArgs);
        return query;
    }

}
